//
//  CZStatus.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZStatus: NSObject {
    /// 微博创建时间
    var created_at: String?
    
    /// 微博ID
    var id: Int64 = 0
    
    /// 微博信息内容
    var text: String?
    
    /// 微博来源
    var source: String?{
        didSet{
            if source == nil || source?.characters.count == 0{
                CZPrint(items: "返回的来源有问题")
                return
            }
            
            let nsSource = source! as NSString
            let firstRange = nsSource.rangeOfString(">")
            let endRange = nsSource.rangeOfString("</")
            let range = NSRange(location: firstRange.location + 1, length: endRange.location - firstRange.location - 1)
            let newSource = nsSource.substringWithRange(range)
            source = "来自" + newSource
        }
    }
    
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数
    var attitudes_count: Int = 0
    
    var user: CZUser?
    var pictureURLs: [NSURL]?
    var retweeted_status: CZStatus?
    
    /// 缓存的行高
    var rowHeight: CGFloat?
    
    /// 微博配图: 数组里面是字典 // Int类型的数组[Int], 字典类型的数组 [String: AnyObject] -> [[String: AnyObject]]
    var pic_urls: [[String: AnyObject]]?{
        didSet{
            guard let urlDicts = pic_urls else{
                print("配图没有值")
                return
            }
            pictureURLs = [NSURL]()
            for dict in urlDicts{
                if let urlString = dict["thumbnail_pic"] as? String {
                    let url = NSURL(string: urlString)
                    pictureURLs?.append(url!)
                }
            }
        }
    }
    
    
    
    init(dict: [String: AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            if let dict = value as? [String:AnyObject]{
                user = CZUser(dict: dict)
            }
            return
        }
        
        if key == "retweeted_status"{
            if let dict = value as? [String: AnyObject]{
                retweeted_status = CZStatus(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    override var description:String{
        get{
            let keys = ["created_at", "id", "text", "source", "reposts_count", "comments_count", "attitudes_count", "pic_urls","user"]
            return"\n\t 微博模型:\(dictionaryWithValuesForKeys(keys))"
        }
    }
}
